package com.applcn.user.controller.merchant;

import com.applcn.core.base.Result;
import com.applcn.user.dto.merchant.*;
import com.applcn.user.service.MerchantService;
import com.applcn.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * com.applcn.user.controller
 * Description:
 *
 * @author: 大妖怪
 * @date: 2020-01-21 07:03
 */
@RestController
@RequestMapping("/api/user/merchant")
@RequiredArgsConstructor
public class MerchantController {

    private final MerchantService merchantService;
    private final UserService userService;

    /**
     * 获取图片验证码
     *
     * @param uuid
     */
    @GetMapping("/captcha/{uuid}")
    public void captcha(@PathVariable("uuid") String uuid, HttpServletResponse response) throws IOException {
        merchantService.captcha(uuid, response);
    }

    /**
     * 获取短信验证码
     *
     * @param phone
     * @param uuid
     * @param code
     * @return
     */
    @GetMapping("/gainSmsCode")
    public Result gainSmsCode(@RequestParam("phone") String phone,
                              @RequestParam("uuid") String uuid,
                              @RequestParam("code") String code) {
        GainSmsCodeDTO dto = GainSmsCodeDTO
                .builder()
                .phone(phone)
                .uuid(uuid)
                .code(code)
                .build();
        return merchantService.sendSmsCode(dto);
    }

    /**
     * 创建商户
     *
     * @param dto
     * @return
     */
    @PostMapping("/createMerchant")
    public Result createMerchant(@RequestBody @Validated CreateMerchantReqDTO dto) {
        return merchantService.createMerchant(dto);
    }

    /**
     * 手机号密码登录
     *
     * @return
     */
    @PostMapping("/loginByPhoneAndPassword")
    public Result loginByPhoneAndPassword(@RequestBody @Validated LoginByPhoneAndPasswordReqDTO dto) {
        return merchantService.loginByPhoneAndPassword(dto);
    }

    /**
     * 短信登录
     *
     * @param phone
     * @param smsCode
     * @return
     */
    @GetMapping("/loginBySmsCode")
    public Result loginBySmsCode(@RequestParam("phone") String phone,
                                 @RequestParam("smsCode") String smsCode) {
        return merchantService.loginBySmsCode(phone, smsCode);
    }

    /**
     * 获取当前登录用户信息
     *
     * @return
     */
    @GetMapping("/getCurrentUserInfo")
    public Result getCurrentUserInfo() {
        return userService.getCurrentUserInfo();
    }

    /**
     * 分页查询管理列表
     *
     * @param pageNum
     * @param pageSize
     * @param nickName
     * @return
     */
    @GetMapping("/pageAdmin")
    public Result pageAdmin(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                            @RequestParam(value = "nickName", required = false) String nickName) {

        PageAdminReqDTO dto = new PageAdminReqDTO();
        dto.setPageNum(pageNum);
        dto.setPageSize(pageSize);
        dto.setNickName(nickName);

        return userService.pageAdmin(dto);
    }

    /**
     * 通过手机号搜索商户
     *
     * @param phone
     * @return
     */
    @GetMapping("/searchAdminByPhone")
    public Result searchAdminByPhone(@RequestParam(value = "phone") String phone) {
        return userService.searchAdminByPhone(phone);
    }

    /**
     * 添加管理员
     *
     * @param addAdminDTO
     * @return
     */
    @PostMapping("/addAdmin")
    public Result addAdmin(@RequestBody @Validated AddAdminDTO addAdminDTO) {
        return userService.addAdmin(addAdminDTO);
    }
}
